package developer.算法.二叉树.二叉树的右视图;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author zhangyongkang
 * @time 2025/3/26 10:54
 * @description 给定一个二叉树的 根节点 root，想象自己站在它的右侧，按照从顶部到底部的顺序，返回从右侧所能看到的节点值。
 * <p>
 * 输入：root = [1,2,3,4,null,null,null,5]
 * <p>
 * 输出：[1,3,4,5]
 */
public class ErChaShuDeYouShiTu {

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    public static void main(String[] args) {
        Solution3 solution = new Solution3();
        TreeNode treeNode = new TreeNode(1, new TreeNode(2, null, new TreeNode(5)), new TreeNode(3, null, new TreeNode(4)));
        List<Integer> integers = solution.rightSideView(treeNode);
        integers.forEach(System.out::println);
    }

    static class Solution3 {
        public List<Integer> rightSideView(TreeNode root) {
            if (root == null) {
                return new ArrayList<>();
            }
            //如果最右边的数据 才会进行展示
            List<Integer> list = new ArrayList<>();
            Queue<TreeNode> stack = new LinkedList<>();
            stack.offer(root);
            while (!stack.isEmpty()) {//一次循环代表一层的数据
                int size = stack.size();//核心点每次只遍历当前层的数据 而不遍历下一层的数据
                for (int i = 0; i < size; i++) {
                    TreeNode currentNode = stack.poll();
                    if (i == size - 1) {
                        list.add(currentNode.val);
                    }
                    if (currentNode.left != null) {
                        stack.offer(currentNode.left);
                    }
                    if (currentNode.right != null) {
                        stack.offer(currentNode.right);
                    }
                }
            }
            return list;

        }

    }

    static class Solution {
        public List<Integer> rightSideView(TreeNode root) {
            if (root == null) return new ArrayList<>();
            Queue<TreeNode> stack = new LinkedList<>();
            stack.offer(root);
            List<Integer> result = new ArrayList<>();
            while (!stack.isEmpty()) {
                int size = stack.size();
                for (int i = 0; i < size; i++) {
                    TreeNode node = stack.poll();
                    if (node.left != null) {
                        stack.offer(node.left);
                    }
                    if (node.right != null) {
                        stack.offer(node.right);
                    }

                    if (i == size - 1) {
                        result.add(node.val);
                    }
                }
            }
            return result;
        }
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
